package com.jsbs.iam.common.core.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import javax.servlet.http.HttpServletRequest;

public class RequestUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(RequestUtils.class);

    public static boolean isAjaxRequest(HttpServletRequest request){
        return (request.getHeader("X-Requested-With") != null && request.getHeader("X-Requested-With").equals("XMLHttpRequest"));
    }

    public static boolean isJsonRequest(HttpServletRequest request) {
        String contentType = request.getContentType();
        if(StringUtils.isNotEmpty(contentType) && contentType.toLowerCase().contains("json")){
            return true;
        }
        return false;
    }

    public static boolean isMultipartRequest(HttpServletRequest request) {
        String contentType = request.getContentType();
        if(StringUtils.isNotEmpty(contentType) && contentType.toLowerCase().contains("multipart")){
            return true;
        }
        return false;
    }

    /**
     * 获取用户IP地址
     * https://www.jianshu.com/p/1e0124de8e02
     * @param request
     * @return
     */
    public static String getClientIp(HttpServletRequest request) {
        if (request == null)
        {
            return "unknown";
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("X-Real-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getRemoteAddr();
        }

        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip);
    }

    /**
     * 判断是否新小程序，这个方法只适用于对于Http请求的流程
     * 而且这是一种不规范的使用方法，切换完成之后要去掉
     * @return
     */
    public static boolean isNewApp(){
        String version = MDC.get("version");
        LOGGER.info("version:" + version);
        if(StringUtils.isEmpty(version) || "-1".equals(version)){
            return false;
        }
        if(version.startsWith("1.1")){
            return true;
        }
        return false;
    }

    /**
     * 从多级反向代理中获得第一个非unknown IP地址
     *
     * @param ip 获得的IP地址
     * @return 第一个非unknown IP地址
     */
    public static String getMultistageReverseProxyIp(String ip)
    {
        // 多级反向代理检测
        if (ip != null && ip.indexOf(",") > 0)
        {
            final String[] ips = ip.trim().split(",");
            for (String subIp : ips)
            {
                if (false == isUnknown(subIp))
                {
                    ip = subIp;
                    break;
                }
            }
        }
        return ip;
    }

    /**
     * 检测给定字符串是否为未知，多用于检测HTTP请求相关
     *
     * @param checkString 被检测的字符串
     * @return 是否未知
     */
    public static boolean isUnknown(String checkString)
    {
        return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
    }


}
